package com.lancoo.znbkxx.teacher.base

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.FrameLayout
import androidx.databinding.DataBindingUtil.inflate
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider


/**
 * @author : Admin
 * @date   : 2019/7/16 19:15
 * @desc   :
 */
abstract class BaseVMFragment<VM : BaseViewModel, DB : ViewDataBinding> : Fragment() {
    lateinit var viewModel: VM
    lateinit var binding: DB
    private var frameLayout: FrameLayout? = null
    open fun providerVMClass(): Class<VM>? = null
    open fun providerContext(): Any? = null

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        providerContext()?.let { initVM(it) }
        binding = inflate(inflater, setLayoutID(), container, false)
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        initView()
        startObserve()
        super.onViewCreated(view, savedInstanceState)
    }

    abstract fun setLayoutID(): Int

    open fun startObserve() {}

    abstract fun initView()

    private fun initVM(T: Any) {
        if (T is Fragment) {
            providerVMClass()?.let {
                viewModel = ViewModelProvider(this).get(it)
                lifecycle.addObserver(viewModel)
            }
        } else {
            providerVMClass()?.let {
                viewModel = ViewModelProvider(requireActivity()).get(it)
                lifecycle.addObserver(viewModel)
            }
        }
    }


    override fun onDestroy() {
        lifecycle.removeObserver(viewModel)
        super.onDestroy()
    }

    open fun openAleterWindow() {   //打开 dailog 窗口 对 dailog 初始化
        frameLayout = FrameLayout(requireContext())
        val params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT)
        (requireActivity().window.decorView as ViewGroup).addView(frameLayout, params)
        getData()
    }

    open fun closeHealthEye() {
        (requireActivity().window.decorView as ViewGroup).removeView(frameLayout)
    }


    open fun getData() {  //获取 存储 sharePrefrence 保存的三原色值
        val realFilter = 20
        val a = (realFilter / 80f * 180).toInt()
        val r = (200 - realFilter / 80f * 190).toInt()
        val g = (180 - realFilter / 80f * 170).toInt()
        val b = (60 - realFilter / 80f * 60).toInt()
        frameLayout!!.setBackgroundColor(Color.argb(a, r, g, b))
    }

    fun showAnimation() {
        val lp = requireActivity().window.attributes
        lp.alpha = 0.8f
        requireActivity().window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        requireActivity().window.attributes = lp
    }

    fun dismissAnimation() {
        val lp = requireActivity().window.attributes
        lp.alpha = 1.0f
        requireActivity().window.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
        requireActivity().window.attributes = lp
    }
}